import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

function delay(duration) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, duration);
  });
}

export default new Vuex.Store({
  state: {
    count: 0,
  },
  getters: {},
  mutations: {
    increment(state) {
      state.count++;
    },
    decrement(state) {
      state.count--;
    },
    power(state, payload) {
      state.count **= payload;
    },
  },
  actions: {
    async asyncIncrement(ctx) {
      await delay(1000);
      ctx.commit("increment");
    },
    async asyncDecrement(ctx) {
      await delay(1000);
      ctx.commit("decrement");
    },
    async asyncPower(ctx, payload) {
      await delay(1000);
      ctx.commit("power", payload);
    },
  },
  modules: {},
});
